<?php defined('SYSPATH') OR die('No direct access allowed.');

class Redbus_Model extends Model {

    const REDBUS_DESTINATION = 'rbdest';
    const REDBUS_BUS = 'rbbus';
    public function getDestinations($source='')
	{
	  $key = Redbus_Model::REDBUS_DESTINATION.$source;
	  $sources = $this->cache->get($key);
	  if ($sources === false) {
    	  $url = 'http://www.redbus.in/GetDestinationsJSON.aspx';
    	  /*if (!empty($source) && $source != '') {
    	    $url = $url . "?sourceid=".$source;
    	  }*/
           $curl = curl_init($url);
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
           $data = curl_exec($curl);
           $data = json_decode($data);            
           //echo print_r($data, true);           
           $sources = $data->sources;
           $this->cache->set($key, $sources, 86400);
	  }
	  return $sources;
	}

    public function getDestinationName($id)
	{
	  $destinations = $this->getDestinations();
	  foreach ($destinations as $destination) {
	    if($destination->id == $id) return $destination;
	  }
	  return null;	
	}
	
	
	public function getAvailableBuses($source,$destination, $doj) {
	   $url = 'http://www.redbus.in/booking/SearchResultsJSON.aspx?';
	   $url = $url . "fromcityid=".$source;
	   $url = $url . "&tocityid=".$destination;
	   $url = $url . "&doj=".$doj;	   
	   $curl = curl_init($url);
	   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	   $data = curl_exec($curl);
	   $data = substr($data, 17);
	   $data = substr($data, 0, strlen($data)-1);
	   curl_close($curl);
	   
	   //////////////JSON Formatting...
	   $objs = json_decode($data);
	   
	   return $objs;	   	   
	} 
	
	public function getBusesAvailableToday($source, $destination) {
	   $doj = date('d-M-Y', time());
	   $keyDate = date('dM', time());
	   $key = Redbus_Model::REDBUS_BUS.$source.'.'.$destination.'.'.$keyDate;
	   $buses = $this->cache->get($key);	   
	   if ($buses === false ) {
    	   $url = 'http://www.redbus.in/booking/SearchResultsJSON.aspx?';
    	   $url = $url . "fromcityid=".$source;
    	   $url = $url . "&tocityid=".$destination;	   	   	   
    	   $curl = curl_init($url."&doj=".$doj);
    	   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	   $data = curl_exec($curl);
    	   $data = substr($data, 17);
    	   $data = substr($data, 0, strlen($data)-1);
    	   curl_close($curl);
    	       	   
    	   //////////////JSON Formatting...
    	   $buses = json_decode($data);
    	       	   
    	   //echo(print_r($buses, TRUE));
    	  //echo "------------2".$doj;   
    	   //////////////////////
    	   $nextDay = date('d-M-Y', time()+86400);	   	   
    	   $curl = curl_init($url."&doj=".$nextDay);
    	   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    	   $data = curl_exec($curl);
    	   $data = substr($data, 17);
    	   $data = substr($data, 0, strlen($data)-1);
    	   curl_close($curl);    	   
    	   
    	   $buses2 = json_decode($data);
    	   
    	   //echo(print_r($buses2, TRUE));
    	   $buses = array_merge($buses, $buses2);
    	   $this->cache->set($key, $buses, 3600);
	   }	   
	   return $buses;	   	   	  
	}

    public function getBusesAvailableTodayCompact($source, $destination) {
      $sourceObj = $this->getDestinationName($source);
      $destinationObj = $this->getDestinationName($destination);
      
      $objs = $this->getBusesAvailableToday($sourceObj->id, $destinationObj->id);
       $detailsNeeded = array();
       foreach ($objs as $obj) {
         $bus = new stdClass();
         $bus->travels = $obj->travelsName;
         $bus->bps = array();
         foreach($obj->BPInformationList as $Bps) {
           $bpDetails = new stdClass();
           $bpDetails->bpTime = $Bps->bpTime;
           $bpDetails->location = $Bps->bpLocation.", ".$sourceObj->value;
           array_push($bus->bps, $bpDetails);
         }
         array_push($detailsNeeded, $bus);	     
       }
      return $detailsNeeded;      
    }
} 
